<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>键盘事件</title>
</head>
<body>
  <input type="text" id="p1">
  <script>
    // 获取输入框
    var p1=document.getElementById('p1');
    // 键盘按下事件
    p1.onkeydown=function(event){
      if(event.keyCode == 13){
        console.log('我按下回车键了');
        console.log(this.value);
      }
      // 持续按下键盘
    p1.onkeypress=function(event){
      console.log('我被一直按着');
      console.log(event.keyCode);
    }
    // 放开键盘
    p1.onkeyup=function(event){
      console.log('我被放开了');
      console.log(event.keyCode);
    }
    }
  </script>
</body>
</html>